#pragma once

#include "../config.h"
#include "../opcode.h"

bool IsRunning();
void CloseGate();

class ServiceManager;

class Gateway : public Connection
{
	friend class SingleTon<Gateway>;

	Gateway();

public:

	enum State
	{
		STATE_NONE = 0,
		STATE_RUN,
		STATE_CLOSING_SERVICE,
		STATE_CLOSED
	};

	virtual ~Gateway();

	void Init(vector<string>& configs);

	virtual int Update();
	virtual void ClosePacket() {}
	virtual void HandleSendSocket(){ SendSelectPacket(); }
	virtual void HandleRecvSocket(){ RecvSelectPacket(); }

	bool SendPacket();
	void ToNetPacket();
	void RecvPacket();

	bool SendSelectPacket();
	void RecvSelectPacket();
	
	int HandleNetPacket();

	void PushSendPacket(NetPacket& packet, BlockTree* tree);

public:

	inline void SetState(State state){ m_state = state; }

private:

	Def		m_def;
	char*	m_ip;
	uint16  m_port;

	State	m_state;

	PakcetQueue		m_waitingQueue;
	ServiceManager* m_serviceManager;
};

#define sGateway  SingleTon<Gateway>::Instance()